﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationService.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//---------------------------------------------------------------------
namespace Deixei.Domain.Service
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Security;
    using Deixei.Domain.Repository;
    using Deixei.Infrastructure;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;

    public class AuthorizationService : IAuthorizationService
    {
        public AuthorizationService()
        {
            Initialize(null, null, null, null);
        }

        public AuthorizationService(
            IUnitOfWork unitOfWork, 
            IWebAppPortfolioRepository webAppPortfolioRepository, 
            IUserRepository userRepository, 
            IRoleRepository roleRepository)
        {
            Initialize(unitOfWork, webAppPortfolioRepository, userRepository, roleRepository);
        }

        [Dependency]
        public IUnitOfWork UnitOfWork { get; set; }

        [Dependency]
        public IWebAppPortfolioRepository WebAppPortfolioRepository { get; set; }

        [Dependency]
        public IUserRepository UserRepository { get; set; }
        
        [Dependency]
        public IRoleRepository RoleRepository { get; set; }


        private void Initialize(IUnitOfWork unitOfWork, IWebAppPortfolioRepository webAppPortfolioRepository, IUserRepository userRepository, IRoleRepository roleRepository)
        {
            UnitOfWork = ( unitOfWork ?? DeixeiContainer.Container.Resolve<UnitOfWork>() );
            WebAppPortfolioRepository = ( webAppPortfolioRepository ?? DeixeiContainer.Container.Resolve<WebAppPortfolioRepository>() );
            UserRepository = ( userRepository ?? DeixeiContainer.Container.Resolve<UserRepository>() );
            RoleRepository = ( roleRepository ?? DeixeiContainer.Container.Resolve<RoleRepository>() );

        }

        public IQueryable<User> GetAllUsers(string ApplicationName)
        { 
            IQueryable<User> data = UserRepository.GetAll().OrderBy(o => o.Username);
            return data;
        }
    }

    public interface IAuthorizationService
    {
        IQueryable<User> GetAllUsers(string ApplicationName);
    }
}
